import Charts // !
import SwiftUI

struct ChartAnnotation : View {
    @State var classroom: Array<Student> = [
        Student(height: 165.0, name: "Foo"),
        Student(height: 150.0, name: "Boo"),
        Student(height: 140.0, name: "John"),
        Student(height: 155.0, name: "Doe")
    ]
    
    var body: some View {
        VStack(spacing: 20) {
            Text("Chart annotation")
                .font(.largeTitle)
            
            VStack {
                Text("Rectangle mark")
                Chart(classroom) { student in
                
                    RectangleMark(
                        x: .value("Height", "\(student.height)"),
                        y: .value("Value", student.height)
                    )
                    .foregroundStyle(Color.purple)
                    .annotation(position: .top, alignment: .center) {
                        Text(String(format: "%.2f", student.height))
                            .foregroundColor(.purple)
                    }
                    
                    RuleMark(
                        y: .value("Min height", 120.0)
                    )
                    .foregroundStyle(.orange)
                    .lineStyle(
                        StrokeStyle(lineWidth: 2, dash: [10])
                    )
                    .annotation(alignment: .trailing) {
                        Text("Min height: 120.0")
                            .font(.system(size: 18))
                            .foregroundColor(.orange)
                    }
                    
                    RuleMark(
                        y: .value("Max height", 200.0)
                    )
                    .foregroundStyle(.orange)
                    .lineStyle(
                        StrokeStyle(lineWidth: 2, dash: [10])
                    )
                    .annotation(alignment: .trailing) {
                        Text("Max height: 200.0")
                            .font(.system(size: 18))
                            .foregroundColor(.orange)
                    }
                    
                }
                .chartYScale(domain: 100...220)
            }
            
        }
        .padding()
    }
}
